﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace VXCompany.SilverlightLogging.Services.Exceptions
{
	public static class ClientExceptionMapper
	{
		/// <summary>
        /// Maps IExceptionMessage to ClientException.
        /// </summary>
        /// <param name="message">The client message to map.</param>
		/// <returns>ClientException from IExceptionMessage.</returns>
		public static Exceptions.ClientException MapMessageToException(ServiceContracts.ExceptionMessage message)
		{
			if (message == null)
			{
				return null;
			}
			ClientException ex = null;

			if (message.Level == ServiceContracts.ErrorLevel.Critical
				|| message.Level == ServiceContracts.ErrorLevel.Error)
			{
				ex = new ClientTechnicalException(message.Message, MapMessageToException(message.InnerException));
			}
			else
			{
				ex = new ClientValidationException(message.Message, MapMessageToException(message.InnerException));
			}

			ex.ClientAddress = message.ClientAddress;
			ex.Level = message.Level;
			ex.UserID = message.UserID;
			ex.ClientApplication = message.ClientApplication;

			return ex;
		}
	}
}